const pageHelper = require("../../../../../helper/page_helper.js");
const ProjectBiz = require("../../../biz/project_biz.js");

import { apiAddVisit } from '../../../../../api/index.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLoad: false,
    formsList: [],
    day: "",
    time: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    ProjectBiz.initPage(this);
    const { day, time } = options;

    this.setData({
      day,
      time
    });

    this._loadDetail();
  },

  _loadDetail: async function () {
    let day = this.data.day;
    if (!day) return;

    let meet = {
      MEET_TITLE: "校园参观",
      dayDesc: this.data.day + " " + this.data.time,
      MEET_MAX_JOIN_CNT: "5",
      MEET_JOIN_FORMS: [
        {
          type: "text",
          title: "姓名",
          max: 5,
          must: true,
          val: ""
        },
        {
          type: "mobile",
          title: "手机号",
          must: true,
          val: ""
        }
      ]
    };
    if (!meet) {
      this.setData({
        isLoad: null
      });
      return;
    }

    this.setData({
      isLoad: true,
      meet
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */


  url: function (e) {
    pageHelper.url(e, this);
  },

  onPageScroll: function (e) {
    // 回页首按钮
    pageHelper.showTopBtn(e, this);
  },

  bindDel: function (e) {
    let idx = pageHelper.dataset(e, "idx");

    let cb = () => {
      let formsList = this.data.formsList;
      formsList.splice(idx, 1);
      this.setData({
        formsList
      });
    };
    pageHelper.showConfirm("确认删除该预约人信息?", cb);
  },

  bindSubmitTap: async function (e) {
    let formsList = this.data.formsList;
    let maxCnt = this.data.meet.MEET_MAX_JOIN_CNT;;
    if (formsList.length == 0) {
      return pageHelper.showModal("请先添加预约人信息");
    }

		 
    if (formsList.length > maxCnt) {
      return pageHelper.showModal("最多可以添加个" + maxCnt + "预约人，请删减");
    }

		const perpleList = formsList.map(v=>{
			return {
				name: v[0].val,
				phone: v[1].val
			}
		})

		const params = {
			date: this.data.meet.dayDesc,
			openId: wx.getStorageSync('openId'),
			persons: perpleList
		}
		console.log('formsList :>> ', params);

		let callback= async ()=>{
			const res = await apiAddVisit(params)

			if (res.code === 200) {
				wx.reLaunch({
					url: pageHelper.fmtURLByPID(
						"/pages/meet/my_join_list/meet_my_join_list"
					)
				});
			}
		}


		pageHelper.showConfirm('确定提交预约', callback);
	
		
  },

  // bindCheckTap: async function (e) {
  //   this.selectComponent("#form-show").checkForms();
  // }
});
